var curShowQuest = null;
var curShowBtn;
var curShowSimplePanel = false;
var isShowQuest = 0;

var questLoadState = [];
var questActionUrl = [];

function docActKey(questId, docId){
    return docId+"_"+questId;
}

function _showQuestAction(questId, docId, btn_n){
    //загрузить квест, если надо
    var key = docActKey(questId, docId);
	if(!questLoadState[key]) {
		questActionUrl[key] = $("#vote_container_"+key).text();
		$("#vote_container_"+key).text("Загрузка");
		var cp = new cpaint();
		cp.call(
			questActionUrl[key],
			'ajax_docAction',
			onLoadQuestAction,
			'','',
			isShowQuest,
			questGetCurBntN(),
			curShowSimplePanel
		);
	}
	$("#vote_container_"+key).show();
}

function questGetCurBntN() {
    var res = 0;
    if (typeof curShowBtn != 'undefined') {
	jQuery.grep(jQuery(curShowBtn).attr('class').split(' '), function (x) {
		//window.console.log(x);
		if (ms = x.match(/^btn_n_(\d+)/)) {
		    res = ms[1];
		}
	});
    }
    return res;
}

function showQuestAction(questId, docId, dom, btn_n){
    var key = docActKey(questId, docId);
	if(curShowQuest !== null) {
		$("#vote_container_"+curShowQuest).hide();
		$(curShowBtn).removeClass('votes-buttons-active');
	}
	if(curShowQuest != key) {
		$(dom).addClass('votes-buttons-active');
		curShowQuest = key;
		curShowSimplePanel = $("#vote_container_"+key).hasClass('votes-window-simple');
		curShowBtn = dom;
        _showQuestAction(questId, docId);
	} else {
		$("#vote_container_"+key).hide();
		curShowQuest = null;
		$(dom).removeClass('votes-buttons-active');
	}
	return false;
}

function onQuestAction(questId,docId){
    var key = docActKey(questId, docId);
	var acts = $("#doc_action_actions_"+key).attr("value");
	var votes = "";
	if(acts.indexOf("v") != -1) {
		var v = [];
		$(".quest_vote_"+questId).each(function(i, inp){
			var res = inp.className.match(/vi_(\d+)/);
			v.push(res[1]+"="+inp.value);
		});
		votes = v.join(",");
	}
	var cp = new cpaint();
	cp.call(
		questActionUrl[key],
		'ajax_docAction',
		onLoadQuestAction,
		acts,
		votes,
		isShowQuest,
		questGetCurBntN(),
		curShowSimplePanel
	);
}

function onLoadQuestAction(result){
	var questId = result.ajaxResponse[0].quest[0].data;
	var docId = result.ajaxResponse[0].doc[0].data;
	var key = docActKey(questId, docId);
	$("#vote_container_"+key).html(result.ajaxResponse[0].content[0].data);
	//если надо - обновить панель
	var panel = result.ajaxResponse[0].panel;
	//if(panel) $("#vote_panel_"+key).html(panel[0].data);
	//если надо - обновить кнопку квеста
	var quest_btn = result.ajaxResponse[0].quest_btn;
	if(quest_btn && $("#quest_btn_"+key).length) $("#quest_btn_"+key).replaceWith(quest_btn[0].data);
	//если надо - обновить кнопку счётчка голосов
	var cur_balls_btn = result.ajaxResponse[0].cur_balls_btn;
	if(cur_balls_btn) { 
	    if ( $("#cur_balls_btn_"+key).length ) $("#cur_balls_btn_"+key).replaceWith(cur_balls_btn[0].data);
	    else $("#quest_balls_"+docId).append(cur_balls_btn[0].data);
	}
	//window.console.log($("#quest_btn_"+key), quest_btn[0].data, key);

    questLoadState[key] = true;
}

function onSelectVoteZv(questId,voteId, ball){
	$(".vote_item_"+questId+"_"+voteId).each(function(i, img){
		var val = img.className.match(/vote_val_(\d*)/)[1];
		img.src = img.src.replace(/_(on|off)/,val<=ball?"_on":"_off");
	});
	$(".quest_vote_"+questId).each(function(i, inp){
		var res = inp.className.match(/vi_(\d+)/);
		if(res && res[1]==voteId) {
			inp.value = ball;
		}
	});
}

function zvezdaOn(dom){
	dom.src = dom.src.replace(/1?\.gif$/,'1.gif');
}

function zvezdaOff(dom){
	dom.src = dom.src.replace(/1?\.gif$/,'.gif');
}

function onSelectVoteBin(questId,voteId, ball){
	$(".quest_vote_"+questId).each(function(i, inp){
		var res = inp.className.match(/vi_(\d+)/);
		if(res && res[1]==voteId) {
			inp.value = ball;
		}
	});
}

function questVoteSubmit(questId,docId,voteId,ball, onAsk,onAsnwer){
    // отправляет результат голосования
    onAsk(questId, voteId, ball);
    var key = docActKey(questId, docId);
    var cp = new cpaint();
    var acts = $("#doc_action_actions_"+key).attr("value");
    if(acts.indexOf("v") != -1) {
	var q = ".quest_vote_"+questId + ".vi_"+voteId;
	var $inp = jQuery(q);
	var vote = "" + voteId + "=" + $inp.val();
	cp.call(
		questActionUrl[key],
		'ajax_docAction',
		onLoadQuestAction,
		acts,
		vote,
		isShowQuest,
		questGetCurBntN(),
		curShowSimplePanel
	);
    }
}

jQuery().ready(function () {
	jQuery('.votes-window-simple').each(function (i, dom) {
	    var ms = jQuery(dom).attr('id').match(/(\d+)_(\d+)$/);
	    //window.console.log(ms);
	    if (ms) {
		var docId = ms[1], questId = ms[2];
		showQuestAction(questId,docId,dom)
	    }
	});
});
